<?php
namespace app\index\controller;

use  think\Controller;

use  think\Loader;
use  think\Db;
use  think\Request;
use  think\cache\driver\Redis;


class Index extends   Controller
{
// 短信验证借口
    public function index()
    {


     $this->sendTemplateSMS("18010653361",array('9527','20'),"1");//手机号码，替换内容数组，模板ID
    }

    private function sendTemplateSMS($to,$datas,$tempId)
    {
        //主帐号,对应开官网发者主账号下的 ACCOUNT SID
        $accountSid= '8a216da85f341b69015f38feb9d70311';

//主帐号令牌,对应官网开发者主账号下的 AUTH TOKEN
        $accountToken= 'f031483b01fe4f0d8c9e33f5cace4c66';

//应用Id，在官网应用列表中点击应用，对应应用详情中的APP ID
//在开发调试的时候，可以使用官网自动为您分配的测试Demo的APP ID
        $appId='8a216da85f341b69015f38febb5a0318';

//请求地址
//沙盒环境（用于应用开发调试）：sandboxapp.cloopen.com
//生产环境（用户应用上线使用）：app.cloopen.com
        $serverIP='app.cloopen.com';


//请求端口，生产环境和沙盒环境一致
        $serverPort='8883';

//REST版本号，在官网文档REST介绍中获得。
        $softVersion='2013-12-26';
        // 初始化REST SDK

        Loader::import("REST.php");
        $rest = new \REST($serverIP,$serverPort,$softVersion);
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);

        // 发送模板短信
        echo "Sending TemplateSMS to $to <br/>";
        $result = $rest->sendTemplateSMS($to,$datas,$tempId);
        if($result == NULL ) {
            echo "result error!";

        }
        if($result->statusCode!=0) {
            echo "error code :" . $result->statusCode . "<br>";
            echo "error msg :" . $result->statusMsg . "<br>";
            //TODO 添加错误处理逻辑
        }else{
            echo "Sendind TemplateSMS success!<br/>";
            // 获取返回信息
            $smsmessage = $result->TemplateSMS;
            echo "dateCreated:".$smsmessage->dateCreated."<br/>";
            echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
            //TODO 添加成功处理逻辑
        }
    }

// 邮件发送；
    public  function getMaile(){
             Loader::import('PHPMailer.PHPMailerAutoload');
        $mail = new \PHPMailer(true); //建立邮件发送类
        $mail->CharSet = "UTF-8";//设置信息的编码类型
        $address = "z869886933@163.com";//收件人地址
        $mail->IsSMTP(); // 使用SMTP方式发送
        $mail->Host = "smtp.163.com"; //使用163邮箱服务器
        $mail->SMTPAuth = true; // 启用SMTP验证功能
        $mail->Username = "qq1142298019@163.com"; //你的163服务器邮箱账号
        $mail->Password = "QQ1355888"; // 163邮箱密码
        $mail->Port = 25;//邮箱服务器端口号
        $mail->From = "qq1142298019@163.com"; //邮件发送者email地址
        $mail->FromName = "阿联酋王子";//发件人名称
        $mail->AddAddress("$address", "张三"); //收件人地址，可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
//        $mail->AddAttachment("D:\abc.txt"); // 添加附件(注意：路径不能有中文)
        $mail->IsHTML(true);//是否使用HTML格式
        $mail->Subject = "老铁起床窝尿了"; //邮件标题
        $mail->Body = "双击66666"; //邮件内容，上面设置HTML，则可以是HTML
        if (!$mail->Send()) {
            echo "邮件发送失败. <p>";
            echo "错误原因: " . $mail->ErrorInfo;
            exit;
        }else {
            echo "邮件发送成功";
        }
    }
//得到首页热搜
    public  function getSearch(){
       $rows=  Db::query("SELECT * FROM  rs_poular");

         $callback=  Request::instance()->param('callback');

      echo $callback.'('.json_encode($rows).')';



    }
//    得到首页公司职业招聘；
    public  function  getPosition(){
        $data['c.areaid']   =     Request::instance()->param("areaid","");
        $data['c.wrokDay']    =  Request::instance()->param("wrokDay","");
        $data['c.fieldName']    =  Request::instance()->param("fieldName","");
        $data['c.studyName']=        Request::instance()->param("studyName","");

//    判断有没有值;

        if($data["c.areaid"]==""){
            unset($data['c.areaid']);
        }
        if($data["c.wrokDay"]==""){
            unset($data['c.wrokDay']);
        }
        if($data["c.fieldName"]==""){
            unset($data['c.fieldName']);
        }
        if($data["c.studyName"]==""){
            unset($data['c.studyName']);
        }

// 当前页数码；
        $pag=Request::instance()->param('page');
        $page['page']=empty($pag)?1:$pag;

//        数据需要显示的page；
        $count =  Db::name('company')->alias("c")->where($data)->count();

        $page['Count']=ceil($count/6);
        if ($page['page']<=1){
            $page['page']=1;
        }elseif ($page['page']>= $page['Count']){
            $page['page']=$page['Count'];
        }
//      每页数据库显示的数据；
        $starPage=( $page['page']-1)*6;


        $page['data']= Db::name('company')->alias("c")->where($data)->join("rs_pc_areas a","c.areaid=a.areaid","LEFT")->limit($starPage,6)->select();
//      总共的页面page数;


        ;

        $callback=  Request::instance()->param('callback');



        echo $callback.'('.json_encode($page).')';

    }
// 得到省份;11111111111111111111111111111111111111hello world
 public  function  getShen(){
         $shen=   Db::name("pc_provinces")->select();
     $callback=  Request::instance()->param('callback');
     echo $callback.'('.json_encode($shen).')';
 }
// 得到城市；
public  function  getCheng(){
    $callback=  Request::instance()->param('callback');
    $date['provinceid']=  Request::instance()->param('shenId');
     $cheng= Db::name('pc_cities')->where($date)->select();
      echo $callback."(".json_encode($cheng).")";
}
//得到区县
    public  function  getqu(){
        $callback=  Request::instance()->param('callback');
        $date['cityid']=  Request::instance()->param('chengId');
        $cheng= Db::name('pc_areas')->where($date)->select();
        echo $callback."(".json_encode($cheng).")";
    }


//    得到搜索的工作经历
    public  function getwrkeDays(){
        $callback=  Request::instance()->param('callback');
           $wrkDay=    Db::name("wrokday")->select();
              echo $callback."(".json_encode($wrkDay).")";
    }
//    得到搜索的学习经历
    public  function  getStudy(){
      $callback=     Request::instance()->param('callback');
        $study=    Db::name("study")->select();
        echo $callback."(".json_encode($study).")";
}
//得到搜索的工作领域;
  public  function  getField(){
      $callback=     Request::instance()->param('callback');
      $filed=    Db::name("fieldwroke")->select();
      echo $callback."(".json_encode($filed).")";
  }

//  一个案例；
public  function  getexample(){
      $data['areaid']   =     Request::instance()->param("areaid","");
     $data['wrokDay']    =  Request::instance()->param("wrokDay","");
     $data['fieldName']    =  Request::instance()->param("fieldName","");
     $data['studyName']=        Request::instance()->param("studyName","");
          $callback     =      Request::instance()->param('callback',"");

    $pag=Request::instance()->param('page');
    $page['page']=empty($pag)?1:$pag;

//        数据需要显示的page；
    $count =  Db::name('company')->where($data)->count();

    $page['Count']=ceil($count/6);
    if ($page['page']<=1){
        $page['page']=1;
    }elseif ($page['page']>= $page['Count']){
        $page['page']=$page['Count'];
    }
//      每页数据库显示的数据；
    $starPage=( $page['page']-1)*6;
  echo    Db::name('company')->where($data)->limit($starPage,6)->fetchSql()->select();

//      总共的页面page数;
    echo $callback."(".json_encode($data).")";


}

    //  获取公司详细信息以及职位招聘信息：

    public  function getcompanyInfo(){
        $data["rs_company.id"]=    Request::instance()->param("id");

        $companyInfo=       Db::name("company")->join("rs_place r","rs_company.id=r.companyId","LEFT")->join("rs_pc_areas a","a.areaid=rs_company.areaid","LEFT")->where($data)->select();

        $callback     =      Request::instance()->param('callback',"");
        if($companyInfo){

            echo $callback."(".json_encode($companyInfo).")";
        }else{
            echo $callback."(0)";
        }


    }
}
